﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bowerbird.Website.Models.ContactData;

namespace Bowerbird.Website.Controllers
{
    public class ContactDataController : ControllerBase
    {
        [Authorize]
        public ActionResult AddNew()
        {
            var contactDataTypes = GetContactDataTypeSelectList();
            //var visibilityOptions = Enum.GetNames(typeof(Visibility));
            var visibilityOptions = new string[] { "none", "everyone", "friends" };

            AddNewContactDataModel model = new AddNewContactDataModel
            {
                ContactDataTypes = contactDataTypes,
                VisibilityOptions = new MultiSelectList(visibilityOptions)
            };

            return View(model);
        }

        [HttpPost]
        [Authorize]
        public ActionResult AddNew(AddNewContactDataModel model)
        {
            if (!ModelState.IsValid)
            {
                var contactDataTypes = GetContactDataTypeSelectList();

                model.ContactDataTypes = contactDataTypes;

                return View(model);
            }

            // ToDo: API Call

            //UserService service = new UserService();

            //service.AddNewContactData(
            //    CurrentUser.Id,
            //    model.ContactDataType,
            //    model.Name,
            //    model.Value,
            //    ConcatenateVisibility(model.VisibleTo));

            return RedirectToAction("Index", "Home");
        }

        [Authorize]
        [HttpGet]
        public ActionResult Remove(string id)
        {
            // ToDo: API Call

            //UserService service = new UserService();

            //service.RemoveContactData(CurrentUser.Id, id);

            return RedirectToAction("Index", "Home");
        }

        private static List<SelectListItem> GetContactDataTypeSelectList()
        {
            var contactDataTypes = new List<SelectListItem>();

            //foreach (var cdt in Enum.GetValues(typeof(ContactDataType)))
            foreach (var cdt in new string[] { "none", "everyone", "friends" })
                contactDataTypes.Add(new SelectListItem { Text = cdt, Value = cdt });

            return contactDataTypes;
        }

        private string ConcatenateVisibility(string[] values)
        {
            string value = string.Empty;

            if (values.Contains("none"))
                value = "none";
            else if (values.Contains("everyone"))
                value = "everyone";
            else
            {
                foreach (var v in values.Distinct())
                    value += v.ToLower() + ";";
            }

            return value;
        }

        private string[] SplitVisibility(string value)
        {
            return value.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
        }
    }
}
